﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for Game
/// </summary>
public class Game
{
    private List<Player> _players;
    private Deck _deck;
    private Dictionary<CardRank, Player> _books;
    private int gameID;

    public Game(string playerName, IEnumerable<string> opponentNames)
	{
        _players = new List<Player>();
        _players.Add(new Player(playerName));
        foreach (string player in opponentNames)
            _players.Add(new Player(player));
        _books = new Dictionary<CardRank, Player>();
        _deck = new Deck();
        Deal();
	}

    private void Deal()
    {
        _deck.Shuffle();
        for (int i = 0; i < 5; i++)
            foreach (Player player in _players)
                player.addCardToHand(_deck.Deal());
        foreach (Player player in _players)
            PullOutBooks(player);
    }

    public List<Card> GetPlayerCardNames()
    {
        return _players[0].GetCardNames();
    }

    public bool PullOutBooks(Player player)
    {
        IEnumerable<CardRank> booksPulled = player.PullOutBooks();
        foreach (CardRank rank in booksPulled)
            _books.Add(rank, player);
        if (player.CardCount == 0)
            return true;
        return false;
    }

    public string DescribeBooks()
    {
        string whoHasWhichBooks = "";
        foreach (CardRank rank in _books.Keys)
            whoHasWhichBooks += _books[rank].Name + " has a book of "
                + Card.Plural(rank) + Environment.NewLine;
        return whoHasWhichBooks;
    }

    public bool Play(int card, string player)
    {
        Player askPlayer = _players.Where(x => x.Name == player).FirstOrDefault();
        CardRank rank = (CardRank)card;

        if (askPlayer.hasCard(rank))
        {
            //add to player hand
            //remove from askPlayer hand
        }
        else
        {
           LogMove(gameID, askPlayer + " doesn't have any " + rank + ". Go Fish!");
            //Get Card
        }

        return false;
    }

    public void LogMove(int gameID, string move)
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
        {
            connection.Open();
            SqlCommand command = new SqlCommand(null, connection);

            // Insert into GameLog
            command.CommandText =
                "INSERT INTO GameLog (GameID, Move) " +
                "VALUES (@gameID, @Move)";
            SqlParameter idParam = new SqlParameter("@gameID", SqlDbType.Int);
            idParam.Value = gameID;
            command.Parameters.Add(idParam);
            SqlParameter moveParam = new SqlParameter("@Move", SqlDbType.Int);
            idParam.Value = move;
            command.Parameters.Add(moveParam);

            // Call Prepare after setting the Commandtext and Parameters.
            command.Prepare();
            command.ExecuteNonQuery();

        }
    }

}